Conditions | 6 |
Paths | 33 |
Total Lines | 64 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /* TODO |
||
160 | app.get('/exams', function (req, res, next) { |
||
161 | if (!req.query.id || !req.query.pwd || (req.query.sem && !(/^20\d{2}-20\d{2}-[1-2]$/).test(req.query.sem))) { |
||
162 | res.send({ error: "参数不正确" }); |
||
163 | return; |
||
164 | } |
||
165 | if (fullLog) { |
||
166 | var start = new Date(); |
||
167 | console.log((timeStamp() + 'Started to query the exams: ').cyan + req.query.id.yellow); |
||
1 ignored issue
–
show
|
|||
168 | } |
||
169 | access.login(req.query.id, req.query.pwd, res, function (headers, ires) { |
||
170 | fullLog && console.log((timeStamp() + 'Successfully logged in.').green); |
||
1 ignored issue
–
show
|
|||
171 | |||
172 | var ret = {}; |
||
173 | var $ = cheerio.load(ires.text); |
||
174 | |||
175 | ret.name = escaper.unescape($('.block1text').html()).match(/姓名:.+</)[0].replace('<', '').substring(3); |
||
176 | ret.id = req.query.id; |
||
177 | ret.sem = req.query.sem || getSem(); |
||
178 | |||
179 | superagent |
||
180 | .post('http://csujwc.its.csu.edu.cn/jsxsd/xsks/xsksap_list') |
||
181 | .set(headers) |
||
182 | .type('form') |
||
183 | .send({ |
||
184 | xqlbmc: '', |
||
185 | xnxqid: ret.sem, |
||
186 | xqlb: '' |
||
187 | }) |
||
188 | .end(function (err, iires) { |
||
189 | if (err) { |
||
190 | console.log((timeStamp() + 'Failed to reach exams page\n' + err.stack).red); |
||
1 ignored issue
–
show
|
|||
191 | res.send({ error: '获取成绩失败' }); |
||
192 | return next(err); |
||
193 | } |
||
194 | fullLog && console.log((timeStamp() + 'Successfully entered exams page.').green); |
||
195 | |||
196 | $ = cheerio.load(iires.text); |
||
197 | |||
198 | ret.exams = {}; |
||
199 | ret['exams-count'] = 0; |
||
200 | |||
201 | $('#dataList tr').each(function (index) { |
||
202 | if (index === 0) { |
||
203 | return; |
||
204 | } |
||
205 | let element = $(this).find('td'); |
||
206 | let title = escaper.unescape(element.eq(3).text()); |
||
207 | |||
208 | let item = {}; |
||
209 | item.time = escaper.unescape(element.eq(4).text()); |
||
210 | item.location = escaper.unescape(element.eq(5).text()); |
||
211 | item.seat = escaper.unescape(element.eq(6).text()); |
||
212 | |||
213 | ret.exams[title] = item; |
||
214 | ret['exams-count']++; |
||
215 | }); |
||
216 | |||
217 | access.logout(headers, res, function() { |
||
218 | res.send(JSON.stringify(ret)); |
||
219 | fullLog && console.log((timeStamp() + 'Successfully logged out: ').green + req.query.id.yellow + (' (processed in ' + (new Date() - start) + 'ms)').green); |
||
1 ignored issue
–
show
|
|||
220 | }); |
||
221 | }); |
||
222 | }); |
||
223 | }); |
||
224 | |||
231 |